package com.atguigu.gulimall.gulimallgateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsConfigurationSource;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
import org.springframework.web.server.ServerWebExchange;

/**
 * 跨域配置类
 *
 * @author wu on 2020/10/28 0028
 */
@Configuration
public class GulimallCrosConfiguration {

    @Bean //Bean表示加入容器中
    public CorsWebFilter corsWebFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        //允许跨域
        corsConfiguration.addAllowedMethod("*");
        corsConfiguration.addAllowedHeader("*");
        corsConfiguration.addAllowedOrigin("*");//允许请求来源
        corsConfiguration.setAllowCredentials(true);//是否允许使用cookie跨域
        source.registerCorsConfiguration("/**", corsConfiguration);//任意路径我们都可以进行跨域配置
        return new CorsWebFilter(source);
    }
}
